package com.web.services;

import com.web.beans.DictionaryType;
import com.web.dao.DictionaryTypeDao;
import com.web.exception.MyException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLException;
import java.util.ArrayList;

@Service
public class DictionaryTypeServicesImp implements DictionaryTypeServices {

    @Autowired
    DictionaryTypeDao dictionaryTypeDao;

    @Override
    public ArrayList<DictionaryType> getAll() {
        return dictionaryTypeDao.getAll();
    }

    @Override
    public DictionaryType get(String id) throws MyException {
        DictionaryType dictionaryType = dictionaryTypeDao.get(id);
        if (dictionaryType == null) {
            throw new MyException("没有查询到数据 ");
        }
        return dictionaryType;
    }

    @Override
    public void add(DictionaryType dictionaryType) throws MyException {
        String name = dictionaryType.getName();
        String description = dictionaryType.getDescription();

        if (ObjectUtils.allNull(name) || ObjectUtils.isEmpty(name)) {
            throw new MyException("名称不能为空");
        }
        if (ObjectUtils.allNull(description) || ObjectUtils.isEmpty(description)) {
            throw new MyException("描述不能为空");
        }

        if (name.trim().length() <= 0 || name.length() > 15) {
            throw new MyException("名称必须在1-15之间");
        }

        if (description.trim().length() <= 0 || description.length() > 50) {
            throw new MyException("描述必须在1-50之间");
        }

        int add = dictionaryTypeDao.add(dictionaryType);
        if (add <= 0) {
            throw new MyException("添加失败");
        }
    }

    @Override
    public void edit(DictionaryType dictionaryType) throws MyException {
        String name = dictionaryType.getName();
        String description = dictionaryType.getDescription();
        if (ObjectUtils.allNull(name) || ObjectUtils.isEmpty(name)) {
            throw new MyException("名称不能为空");
        }
        if (ObjectUtils.allNull(description) || ObjectUtils.isEmpty(description)) {
            throw new MyException("描述不能为空");
        }
        if (name.trim().length() <= 0 || name.trim().length() > 20) {
            throw new MyException("名称只能是1-20不能为空");
        }
        if (description.trim().length() <= 0 || description.trim().length() > 50) {
            throw new MyException("描述只能是1-50不能为空");
        }
        int edit = dictionaryTypeDao.edit(dictionaryType);
        if (edit <= 0) {
            throw new MyException("字典类型修改失败");
        }
    }

    @Override
    public void del(String id) throws MyException {
        if (ObjectUtils.allNull(id) || ObjectUtils.isEmpty(id)) {
            throw new MyException("id不能为空");
        }
        int del = 0;
        try {
            del = dictionaryTypeDao.del(id);
        } catch (Exception e) {
            if (e.getMessage().contains("a foreign key constraint fails")) {
                throw new MyException("当前类型含有子数据，无法删除");
            }
        }

        if (del <= 0) {
            throw new MyException("字典类型删除失败");
        }
    }
}
